package cn.iocoder.yudao.module.worker.service.trendlike;

import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
import cn.iocoder.yudao.module.worker.controller.app.trendlike.vo.AppTrendLikePageReqVO;
import cn.iocoder.yudao.module.worker.controller.app.trendlike.vo.AppTrendLikeSaveReqVO;
import cn.iocoder.yudao.module.worker.dal.mysql.trend.WorkerTrendMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;

import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import cn.iocoder.yudao.module.worker.controller.admin.trendlike.vo.*;
import cn.iocoder.yudao.module.worker.dal.dataobject.trendlike.TrendLikeDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.worker.dal.mysql.trendlike.TrendLikeMapper;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.worker.enums.ErrorCodeConstants.*;

/**
 * 动态点赞 Service 实现类
 *
 * @author Jerry
 */
@Service
@Validated
public class TrendLikeServiceImpl implements TrendLikeService {

    @Resource
    private TrendLikeMapper trendLikeMapper;

    @Resource
    private WorkerTrendMapper workerTrendMapper;

    @Override
    public Long createTrendLike(TrendLikeSaveReqVO createReqVO) {
        // 插入
        TrendLikeDO trendLike = BeanUtils.toBean(createReqVO, TrendLikeDO.class);
        trendLikeMapper.insert(trendLike);
        // 返回
        return trendLike.getId();
    }

    @Override
    public void updateTrendLike(TrendLikeSaveReqVO updateReqVO) {
        // 校验存在
        validateTrendLikeExists(updateReqVO.getId());
        // 更新
        TrendLikeDO updateObj = BeanUtils.toBean(updateReqVO, TrendLikeDO.class);
        trendLikeMapper.updateById(updateObj);
    }

    @Override
    public void deleteTrendLike(Long id) {
        // 校验存在
        validateTrendLikeExists(id);
        // 删除
        trendLikeMapper.deleteById(id);
    }

    private void validateTrendLikeExists(Long id) {
        if (trendLikeMapper.selectById(id) == null) {
            throw exception(TREND_LIKE_NOT_EXISTS);
        }
    }

    @Override
    public TrendLikeDO getTrendLike(Long id) {
        return trendLikeMapper.selectById(id);
    }

    @Override
    public PageResult<TrendLikeDO> getTrendLikePage(TrendLikePageReqVO pageReqVO) {
        return trendLikeMapper.selectPage(pageReqVO);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long createTrendLike(AppTrendLikeSaveReqVO createReqVO) {
        TrendLikeDO trendLikeDO = trendLikeMapper.getTrendLike(createReqVO.getUserId(), createReqVO.getTrendId());
        // 插入
        if(trendLikeDO == null){
            trendLikeDO = BeanUtils.toBean(createReqVO, TrendLikeDO.class);
            trendLikeMapper.insert(trendLikeDO);
        }else{
            trendLikeMapper.deleteById(trendLikeDO);
        }

        // TODO 统计点赞数量
        Integer likeCount = trendLikeMapper.getTrendLikeCount(createReqVO.getTrendId());
        workerTrendMapper.updateLikeCount(createReqVO.getTrendId(), likeCount);
        // 返回
        return trendLikeDO.getId();
    }

    @Override
    public void updateTrendLike(AppTrendLikeSaveReqVO updateReqVO) {
        // 校验存在
        validateTrendLikeExists(updateReqVO.getId());
        // 更新
        TrendLikeDO updateObj = BeanUtils.toBean(updateReqVO, TrendLikeDO.class);
        trendLikeMapper.updateById(updateObj);
    }

    @Override
    public PageResult<TrendLikeDO> getTrendLikePage(AppTrendLikePageReqVO pageReqVO) {
        return trendLikeMapper.selectPage(pageReqVO);
    }

    @Override
    public List<TrendLikeDO> getTrendLikeList(Long userId, Set<Long> trendIds) {
        if(userId == null || CollectionUtils.isAnyEmpty(trendIds)){
            return CollectionUtils.newArrayList(new ArrayList<>());
        }
        return trendLikeMapper.getTrendLikeList(userId, trendIds);
    }

}